package com.zhu.controller;

import com.zhu.pojo.Expend;
import com.zhu.pojo.TransSum;
import com.zhu.pojo.Transform;
import com.zhu.service.ExpendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.List;
import java.util.Map;

@RestController
public class ExpendController {
    @Autowired
    ExpendService expendService;

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/info/{username}")
    public List<Transform> list(@PathVariable("username") String username){
        return expendService.getAll(username);
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/adminEx")
    public List<Expend> listByAdmin(){
        return expendService.getAllByAdmin();
    }


    @CrossOrigin //解决跨域问题
    @PostMapping("/api/queryEx")
    public List<Transform> queryList(@RequestBody Expend ex) {
        return expendService.getByCondition(ex);
    }


    @CrossOrigin //解决跨域问题
    @PostMapping("/api/queryAdminEx")
    public List<Expend> queryListByAdmin(@RequestBody Expend ex) {
        return expendService.getByAdmin(ex);
    }

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/save")
    public String save(@RequestBody Expend ex) {
        boolean flag = expendService.save(ex);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/upd")
    public String upd(@RequestBody Expend ex) {
        boolean flag = expendService.upd(ex);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/del/{id}")
    public String upd(@PathVariable("id") int id) {
        boolean flag = expendService.del(id);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }


    @CrossOrigin //解决跨域问题
    @PostMapping("/api/drawEx")
    public List<Map<Object,Object>> draw(@RequestBody TransSum tr) {

        return expendService.getNumAndName(tr);
    }

}

